
#include <BOpenGL>

#include "member_BUniform.h"

using namespace BWE;

member_BUniform::member_BUniform(BUniform* uniform)
{
	boss = uniform;
	location = -1;
	dirty = true;
}
member_BUniform::~member_BUniform()
{

}

bool member_BUniform::fresh(GLuint program)
{
	if (dirty && location >= 0)
	{
		switch (type)
		{
		case BUniform::Type_Int:
		{
			int* data = (int*)value.data();
			glUniform1iv(location, size, (GLint*)data);
			break;
		}
		case BUniform::Type_UInt:
		{
			unsigned int* data = (unsigned int*)value.data();
			glUniform1uiv(location, size, (GLuint*)data);
			break;
		}
		case BUniform::Type_Float:
		{
			float* data = (float*)value.data();
			glUniform1fv(location, size, (GLfloat*)data);
			break;
		}
		case BUniform::Type_Double:
		{
			double* data = (double*)value.data();
			glUniform1dv(location, size, (GLdouble*)data);
			break;
		}
		case BUniform::Type_Vec3f:
		{
			BVector* data = (BVector*)value.data();
			glUniform3fv(location, size, (GLfloat*)data);
			break;
		}
		case BUniform::Type_Vec4f:
		{
			BColor* data = (BColor*)value.data();
			glUniform4fv(location, size, (GLfloat*)data);
			break;
		}
		case BUniform::Type_Vec3d:
		{
			BVectord* data = (BVectord*)value.data();
			glUniform3dv(location, size, (GLdouble*)data);
			break;
		}
		case BUniform::Type_Vec4d:
		{
			double* data = (double*)value.data();
			glUniform4dv(location, size, (GLdouble*)data);
			break;
		}
		case BUniform::Type_Matrixf:
			break;
		case BUniform::Type_Matrixd:
			break;
		default:
			break;
		}
		dirty = false;
	}
	return false;
}
void member_BUniform::init(BUniform::Type type, int size)
{
	this->type = type;
	this->size = size;
	if (size < 0)
		return;
	switch (type)
	{
	case BUniform::Type_Int:
		value.resize(sizeof(int) * size);
		break;
	case BUniform::Type_UInt:
		value.resize(sizeof(unsigned int) * size);
		break;
	case BUniform::Type_Float:
		value.resize(sizeof(float) * size);
		break;
	case BUniform::Type_Double:
		value.resize(sizeof(double) * size);
		break;
	case BUniform::Type_Vec3f:
		value.resize(sizeof(BVectorf) * size);
		break;
	case BUniform::Type_Vec4f:
		value.resize(sizeof(BColorf) * size);
		break;
	case BUniform::Type_Vec3d:
		value.resize(sizeof(BVectord) * size);
		break;
	case BUniform::Type_Vec4d:
		value.resize(sizeof(double) * 4 * size);
		break;
	case BUniform::Type_Matrixf:
		value.resize(sizeof(BMatrixf) * size);
		break;
	case BUniform::Type_Matrixd:
		value.resize(sizeof(BMatrixd) * size);
		break;
	default:
		break;
	}
	dirty = true;
}
